package animal;

import java.util.ArrayList;
import java.util.List;

public class ZooComposition implements DataAccess, DisplayInformation {
    private String zooName;
    private List<VisitorAdult> zooVisitorList;
    private List<VisitorChild> zooVisitorList2;
    public ZooComposition() {
        this.zooVisitorList = new ArrayList<>();
        this.zooVisitorList2 = new ArrayList<>();
    }

    public ZooComposition(List<VisitorAdult> visitorList) {
        this.zooVisitorList = visitorList;
    }

    @Override
    public boolean addAdult(VisitorAdult adult) {
        return this.zooVisitorList.add(adult);
    }

    @Override
    public boolean deleteAdult(VisitorAdult adult) {
        return this.zooVisitorList.remove(adult);
    }

    @Override
    public boolean deleteAdult(String adultName) {
        return this.zooVisitorList.remove(findAdult(adultName));
    }

    @Override
    public boolean updateAdult(VisitorAdult oldAdult, VisitorAdult newAdult) {
        if (this.zooVisitorList.contains(oldAdult)) {
            this.zooVisitorList.remove(oldAdult);
            return this.zooVisitorList.add(newAdult);
        }
        return false;
    }

    @Override
    public VisitorAdult findAdult(String adultName) {
        VisitorAdult foundAdult = null;
        for (Visitor visitor : this.zooVisitorList) {
            if (visitor instanceof VisitorAdult && visitor.getVisitorName().equals(adultName)) {
                foundAdult = (VisitorAdult) visitor;
                break;
            }
        }
        return foundAdult;
    }

    @Override
    public String displayAllVisitors() {
        StringBuilder displayMessage = new StringBuilder();
        displayMessage.append("All Visitors in the Zoo:\n");
        for (Visitor visitor : this.zooVisitorList) {
            displayMessage.append(visitor.getVisitorName()).append("\n");
        }
        for (Visitor visitor : this.zooVisitorList2) {
            displayMessage.append(visitor.getVisitorName()).append("\n");
        }
        return displayMessage.toString();
    }

    @Override
    public boolean addChild(VisitorChild child) {
        return this.zooVisitorList2.add(child);
    }

    @Override
    public boolean deleteChild(VisitorChild child) {
        return this.zooVisitorList2.remove(child);
    }

    @Override
    public boolean deleteChild(String childName) {
        return this.zooVisitorList2.remove(findChild(childName));
    }

    @Override
    public boolean updateChild(VisitorChild oldChild, VisitorChild newChild) {
        if (this.zooVisitorList2.contains(oldChild)) {
            this.zooVisitorList2.remove(oldChild);
            return this.zooVisitorList2.add(newChild);
        }
        return false;
    }

    @Override
    public VisitorChild findChild(String childName) {
        VisitorChild foundChild = null;
        for (Visitor visitor : this.zooVisitorList2) {
            if (visitor instanceof VisitorChild && visitor.getVisitorName().equals(childName)) {
                foundChild = (VisitorChild) visitor;
                break;
            }
        }
        return foundChild;
    }
}